Data Science Academy - Deep Learning II

Regularização

L1e L2 no TensorFlow

Adicionando Regularização L1 e L2 do Modelo de Rede Neural com TensorFlow: http://tensorlayer.readthedocs.io/en/stable/modules/cost.html.


In [1]:
########################################
# TensorFlow com L1/L2 Para Regressão
########################################
%matplotlib inline
from matplotlib.pyplot import figure, show
import tensorflow as tf
from sklearn.model_selection import train_test_split
import pandas as pd
import os
import numpy as np
from sklearn import metrics
from scipy.stats import zscore
from keras.callbacks import EarlyStopping
from keras.layers import Dense, Dropout
from keras import regularizers
from keras.models import Sequential


Using TensorFlow backend.

In [2]:
# Carregando os Dados e difinindo o nível de log
path = "./data/"

filename_read = os.path.join(path,"auto-mpg.csv")
df = pd.read_csv(filename_read,na_values=['NA','?'])

# Define o nível de saída TensorFlow desejado para este exemplo
tf.logging.set_verbosity(tf.logging.ERROR)

In [3]:
# Função Auxiliares

# Converte todos os valores faltantes na coluna especificada para a mediana
def missing_median(df, name):
    med = df[name].median()
    df[name] = df[name].fillna(med)


# Converte um dataframe Pandas para as entradas x, y que o TensorFlow precisa
def to_xy(df, target):
    result = []
    for x in df.columns:
        if x != target:
            result.append(x)
    # Descobre o tipo da coluna de destino. 
    target_type = df[target].dtypes
    target_type = target_type[0] if hasattr(target_type, '__iter__') else target_type
    # Encoding para int. TensorFlow gosta de 32 bits.
    if target_type in (np.int64, np.int32):
        # Classificação
        dummies = pd.get_dummies(df[target])
        return df.as_matrix(result).astype(np.float32), dummies.as_matrix().astype(np.float32)
    else:
        # Regressão
        return df.as_matrix(result).astype(np.float32), df.as_matrix([target]).astype(np.float32)

In [4]:
# Pré-processaento nos dadod
df.drop('name', 1, inplace = True)
missing_median(df, 'horsepower')
x,y = to_xy(df,"mpg")

In [5]:
print(x)


[[   8.          307.          130.         ...,   12.           70.            1.        ]
 [   8.          350.          165.         ...,   11.5          70.            1.        ]
 [   8.          318.          150.         ...,   11.           70.            1.        ]
 ..., 
 [   4.          135.           84.         ...,   11.60000038   82.            1.        ]
 [   4.          120.           79.         ...,   18.60000038   82.            1.        ]
 [   4.          119.           82.         ...,   19.39999962   82.            1.        ]]

In [6]:
print(y)


[[ 18.        ]
 [ 15.        ]
 [ 18.        ]
 [ 16.        ]
 [ 17.        ]
 [ 15.        ]
 [ 14.        ]
 [ 14.        ]
 [ 14.        ]
 [ 15.        ]
 [ 15.        ]
 [ 14.        ]
 [ 15.        ]
 [ 14.        ]
 [ 24.        ]
 [ 22.        ]
 [ 18.        ]
 [ 21.        ]
 [ 27.        ]
 [ 26.        ]
 [ 25.        ]
 [ 24.        ]
 [ 25.        ]
 [ 26.        ]
 [ 21.        ]
 [ 10.        ]
 [ 10.        ]
 [ 11.        ]
 [  9.        ]
 [ 27.        ]
 [ 28.        ]
 [ 25.        ]
 [ 25.        ]
 [ 19.        ]
 [ 16.        ]
 [ 17.        ]
 [ 19.        ]
 [ 18.        ]
 [ 14.        ]
 [ 14.        ]
 [ 14.        ]
 [ 14.        ]
 [ 12.        ]
 [ 13.        ]
 [ 13.        ]
 [ 18.        ]
 [ 22.        ]
 [ 19.        ]
 [ 18.        ]
 [ 23.        ]
 [ 28.        ]
 [ 30.        ]
 [ 30.        ]
 [ 31.        ]
 [ 35.        ]
 [ 27.        ]
 [ 26.        ]
 [ 24.        ]
 [ 25.        ]
 [ 23.        ]
 [ 20.        ]
 [ 21.        ]
 [ 13.        ]
 [ 14.        ]
 [ 15.        ]
 [ 14.        ]
 [ 17.        ]
 [ 11.        ]
 [ 13.        ]
 [ 12.        ]
 [ 13.        ]
 [ 19.        ]
 [ 15.        ]
 [ 13.        ]
 [ 13.        ]
 [ 14.        ]
 [ 18.        ]
 [ 22.        ]
 [ 21.        ]
 [ 26.        ]
 [ 22.        ]
 [ 28.        ]
 [ 23.        ]
 [ 28.        ]
 [ 27.        ]
 [ 13.        ]
 [ 14.        ]
 [ 13.        ]
 [ 14.        ]
 [ 15.        ]
 [ 12.        ]
 [ 13.        ]
 [ 13.        ]
 [ 14.        ]
 [ 13.        ]
 [ 12.        ]
 [ 13.        ]
 [ 18.        ]
 [ 16.        ]
 [ 18.        ]
 [ 18.        ]
 [ 23.        ]
 [ 26.        ]
 [ 11.        ]
 [ 12.        ]
 [ 13.        ]
 [ 12.        ]
 [ 18.        ]
 [ 20.        ]
 [ 21.        ]
 [ 22.        ]
 [ 18.        ]
 [ 19.        ]
 [ 21.        ]
 [ 26.        ]
 [ 15.        ]
 [ 16.        ]
 [ 29.        ]
 [ 24.        ]
 [ 20.        ]
 [ 19.        ]
 [ 15.        ]
 [ 24.        ]
 [ 20.        ]
 [ 11.        ]
 [ 20.        ]
 [ 21.        ]
 [ 19.        ]
 [ 15.        ]
 [ 31.        ]
 [ 26.        ]
 [ 32.        ]
 [ 25.        ]
 [ 16.        ]
 [ 16.        ]
 [ 18.        ]
 [ 16.        ]
 [ 13.        ]
 [ 14.        ]
 [ 14.        ]
 [ 14.        ]
 [ 29.        ]
 [ 26.        ]
 [ 26.        ]
 [ 31.        ]
 [ 32.        ]
 [ 28.        ]
 [ 24.        ]
 [ 26.        ]
 [ 24.        ]
 [ 26.        ]
 [ 31.        ]
 [ 19.        ]
 [ 18.        ]
 [ 15.        ]
 [ 15.        ]
 [ 16.        ]
 [ 15.        ]
 [ 16.        ]
 [ 14.        ]
 [ 17.        ]
 [ 16.        ]
 [ 15.        ]
 [ 18.        ]
 [ 21.        ]
 [ 20.        ]
 [ 13.        ]
 [ 29.        ]
 [ 23.        ]
 [ 20.        ]
 [ 23.        ]
 [ 24.        ]
 [ 25.        ]
 [ 24.        ]
 [ 18.        ]
 [ 29.        ]
 [ 19.        ]
 [ 23.        ]
 [ 23.        ]
 [ 22.        ]
 [ 25.        ]
 [ 33.        ]
 [ 28.        ]
 [ 25.        ]
 [ 25.        ]
 [ 26.        ]
 [ 27.        ]
 [ 17.5       ]
 [ 16.        ]
 [ 15.5       ]
 [ 14.5       ]
 [ 22.        ]
 [ 22.        ]
 [ 24.        ]
 [ 22.5       ]
 [ 29.        ]
 [ 24.5       ]
 [ 29.        ]
 [ 33.        ]
 [ 20.        ]
 [ 18.        ]
 [ 18.5       ]
 [ 17.5       ]
 [ 29.5       ]
 [ 32.        ]
 [ 28.        ]
 [ 26.5       ]
 [ 20.        ]
 [ 13.        ]
 [ 19.        ]
 [ 19.        ]
 [ 16.5       ]
 [ 16.5       ]
 [ 13.        ]
 [ 13.        ]
 [ 13.        ]
 [ 31.5       ]
 [ 30.        ]
 [ 36.        ]
 [ 25.5       ]
 [ 33.5       ]
 [ 17.5       ]
 [ 17.        ]
 [ 15.5       ]
 [ 15.        ]
 [ 17.5       ]
 [ 20.5       ]
 [ 19.        ]
 [ 18.5       ]
 [ 16.        ]
 [ 15.5       ]
 [ 15.5       ]
 [ 16.        ]
 [ 29.        ]
 [ 24.5       ]
 [ 26.        ]
 [ 25.5       ]
 [ 30.5       ]
 [ 33.5       ]
 [ 30.        ]
 [ 30.5       ]
 [ 22.        ]
 [ 21.5       ]
 [ 21.5       ]
 [ 43.09999847]
 [ 36.09999847]
 [ 32.79999924]
 [ 39.40000153]
 [ 36.09999847]
 [ 19.89999962]
 [ 19.39999962]
 [ 20.20000076]
 [ 19.20000076]
 [ 20.5       ]
 [ 20.20000076]
 [ 25.10000038]
 [ 20.5       ]
 [ 19.39999962]
 [ 20.60000038]
 [ 20.79999924]
 [ 18.60000038]
 [ 18.10000038]
 [ 19.20000076]
 [ 17.70000076]
 [ 18.10000038]
 [ 17.5       ]
 [ 30.        ]
 [ 27.5       ]
 [ 27.20000076]
 [ 30.89999962]
 [ 21.10000038]
 [ 23.20000076]
 [ 23.79999924]
 [ 23.89999962]
 [ 20.29999924]
 [ 17.        ]
 [ 21.60000038]
 [ 16.20000076]
 [ 31.5       ]
 [ 29.5       ]
 [ 21.5       ]
 [ 19.79999924]
 [ 22.29999924]
 [ 20.20000076]
 [ 20.60000038]
 [ 17.        ]
 [ 17.60000038]
 [ 16.5       ]
 [ 18.20000076]
 [ 16.89999962]
 [ 15.5       ]
 [ 19.20000076]
 [ 18.5       ]
 [ 31.89999962]
 [ 34.09999847]
 [ 35.70000076]
 [ 27.39999962]
 [ 25.39999962]
 [ 23.        ]
 [ 27.20000076]
 [ 23.89999962]
 [ 34.20000076]
 [ 34.5       ]
 [ 31.79999924]
 [ 37.29999924]
 [ 28.39999962]
 [ 28.79999924]
 [ 26.79999924]
 [ 33.5       ]
 [ 41.5       ]
 [ 38.09999847]
 [ 32.09999847]
 [ 37.20000076]
 [ 28.        ]
 [ 26.39999962]
 [ 24.29999924]
 [ 19.10000038]
 [ 34.29999924]
 [ 29.79999924]
 [ 31.29999924]
 [ 37.        ]
 [ 32.20000076]
 [ 46.59999847]
 [ 27.89999962]
 [ 40.79999924]
 [ 44.29999924]
 [ 43.40000153]
 [ 36.40000153]
 [ 30.        ]
 [ 44.59999847]
 [ 40.90000153]
 [ 33.79999924]
 [ 29.79999924]
 [ 32.70000076]
 [ 23.70000076]
 [ 35.        ]
 [ 23.60000038]
 [ 32.40000153]
 [ 27.20000076]
 [ 26.60000038]
 [ 25.79999924]
 [ 23.5       ]
 [ 30.        ]
 [ 39.09999847]
 [ 39.        ]
 [ 35.09999847]
 [ 32.29999924]
 [ 37.        ]
 [ 37.70000076]
 [ 34.09999847]
 [ 34.70000076]
 [ 34.40000153]
 [ 29.89999962]
 [ 33.        ]
 [ 34.5       ]
 [ 33.70000076]
 [ 32.40000153]
 [ 32.90000153]
 [ 31.60000038]
 [ 28.10000038]
 [ 30.70000076]
 [ 25.39999962]
 [ 24.20000076]
 [ 22.39999962]
 [ 26.60000038]
 [ 20.20000076]
 [ 17.60000038]
 [ 28.        ]
 [ 27.        ]
 [ 34.        ]
 [ 31.        ]
 [ 29.        ]
 [ 27.        ]
 [ 24.        ]
 [ 23.        ]
 [ 36.        ]
 [ 37.        ]
 [ 31.        ]
 [ 38.        ]
 [ 36.        ]
 [ 36.        ]
 [ 36.        ]
 [ 34.        ]
 [ 38.        ]
 [ 32.        ]
 [ 38.        ]
 [ 25.        ]
 [ 38.        ]
 [ 26.        ]
 [ 22.        ]
 [ 32.        ]
 [ 36.        ]
 [ 27.        ]
 [ 27.        ]
 [ 44.        ]
 [ 32.        ]
 [ 28.        ]
 [ 31.        ]]

In [7]:
# Split em treino e teste
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.25, random_state = 45)

In [8]:
# Criação do Modelo
# https://keras.io/layers/core/
model = Sequential()
model.add(Dense(50, input_dim = x.shape[1], kernel_initializer = 'normal', activation = 'relu'))
model.add(Dense(25, input_dim = x.shape[1], kernel_initializer = 'normal', activation = 'relu'))

model.add(Dense(10, input_dim = 64, 
                kernel_regularizer = regularizers.l2(0.01), 
                activity_regularizer = regularizers.l1(0.01),
                activation = 'relu'))

model.add(Dense(1, kernel_initializer = 'normal'))

# Compilação
model.compile(loss = 'mean_squared_error', optimizer = 'adam')

# Early Stopping
monitor = EarlyStopping(monitor='val_loss', min_delta=1e-3, patience=5, verbose=1, mode='auto')

# Fit
model.fit(x,y,validation_data=(x_test,y_test),callbacks=[monitor],verbose=0,epochs=1000)

# Previsões
pred = model.predict(x_test)


Epoch 00119: early stopping

In [9]:
# Measure RMSE error.  RMSE is common for regression.
score = np.sqrt(metrics.mean_squared_error(pred,y_test))
print("Final score (RMSE): {}".format(score))


Final score (RMSE): 2.9664759635925293

Fim